package com.myway.core.jersey.client;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @ClassName: HttpClient <br/>
 * @Description: request http client <br/>
 * @CreateDate: 2016年4月27日 下午5:25:15 <br/>
 * @author Toby
 * @version V1.0
 */
public class HttpClient {
		/**
		 * @author jiangyang
		 * @description 
		 * 
		 */
		public static String post(String path,String json,String dataType) {
			try {
				URL url = new URL(path);
				HttpURLConnection connection=null;
				connection = (HttpURLConnection) url.openConnection();
				connection.setDoInput(true);
				connection.setDoOutput(true);
				connection.setRequestMethod("POST");
				connection.setUseCaches(false);
				//设置http请求的数据类型
				connection.setRequestProperty("Content-Type", dataType);
				connection.setRequestProperty("client-name", "yang");
				connection.connect();
				OutputStream out = connection.getOutputStream();
				Writer wout = new OutputStreamWriter(out, "utf-8");
				wout.write(json);
				wout.flush();
				wout.close();

				
				InputStream in = connection.getInputStream();
				ByteArrayOutputStream bos = new ByteArrayOutputStream();
				byte[] b = new byte[1024];
				int i;
				while ((i = in.read(b)) != -1)
					bos.write(b, 0, i);
				in.close();
				
				return new String(bos.toByteArray(), "utf-8");
			} catch (Exception e) {
				return "{\"error\":\""+e.getMessage()+"\"}";
			}
		}
		public static String post(String path,String json, boolean isproxy,boolean isasin) {
			return post(path,json,"text/html");
		}
		
		public static void main(String[] args) {
//			HttpClient.post("http://127.0.0.1:8081/checkin/v1/excessBagPay/reciver", "", "text/xml");
			String xml="<?xml version=\"1.0\" encoding=\"UTF-8\"?><Details><Detail><CmdId>PAYORD</CmdId><RespCode>000000</RespCode><ErrorDesc>非支付宝请求</ErrorDesc><SCCode>CCSC1001</SCCode><SubSCCode>SUBPSSSC1001</SubSCCode><PCCode>ALIPAY</PCCode><SubPCCode>SUBALIPAYWP6</SubPCCode><SCOrdId>5112064</SCOrdId><SCAcctDate>20170306</SCAcctDate><SysSeqId>2017030600413944</SysSeqId><AcctDate>20170306</AcctDate><SysTime>134528</SysTime><ActualPayAmt>0.01</ActualPayAmt><CurCode>CNY</CurCode><BankCode> </BankCode><PCSysSeqId>2017030621001004350293937128</PCSysSeqId><PCAcctDate>20170306</PCAcctDate><Resv1></Resv1><Resv2>ticket|1</Resv2><Resv3></Resv3><Resv4></Resv4><Resv5></Resv5><Resv6></Resv6><SignType>MD5</SignType><SignValue>DE9A4A322A995E67FB2ACDBC5D71B142</SignValue></Detail></Details>";
			String result=HttpClient.post("http://127.0.0.1:8080/checkin/v1/excessBagPay/reciver",xml,"application/xml"); 
			System.out.println(result);
		}
}
